package com.qiangesoft.blog.controller.manage;

import com.qiangesoft.blog.common.annotation.OperationLog;
import com.qiangesoft.blog.common.mvc.PageQuery;
import com.qiangesoft.blog.common.mvc.PageResultVO;
import com.qiangesoft.blog.system.authentication.satoken.OnlineUser;
import com.qiangesoft.blog.system.entity.SysMenu;
import com.qiangesoft.blog.system.entity.SysUser;
import com.qiangesoft.blog.system.pojo.dto.PasswordDTO;
import com.qiangesoft.blog.system.pojo.dto.UserSaveDTO;
import com.qiangesoft.blog.system.pojo.vo.SysUserDetailVO;
import com.qiangesoft.blog.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户管理 前端控制器
 * </p>
 *
 * @author qiange
 */
@RestController
@RequestMapping("/system/user")
@Api(tags = "用户管理")
@RequiredArgsConstructor
public class SysUserController {

    private final ISysUserService userService;

    @GetMapping
    @ApiOperation(value = "用户列表")
    public PageResultVO<SysUser> list(PageQuery pageQuery, String username, Boolean state) {
        return userService.listUser(pageQuery, username, state);
    }

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "用户详情")
    public SysUserDetailVO getUserById(@PathVariable Long id) {
        return userService.getUserById(id);
    }

    @PostMapping
    @ApiOperation(value = "添加用户")
    @OperationLog(value = "添加用户")
    public void insert(@RequestBody @Validated UserSaveDTO userSave) {
        userService.insertUser(userSave);
    }

    @PutMapping(value = "/{id}")
    @ApiOperation(value = "修改用户")
    @OperationLog(value = "修改用户")
    public void update(@PathVariable Long id, @RequestBody @Validated UserSaveDTO userSave) {
        userService.updateUser(id, userSave);
    }

    @DeleteMapping(value = "/remove")
    @ApiOperation(value = "删除用户")
    @OperationLog(value = "删除用户")
    public void deleteBatch(@RequestBody List<Long> ids) {
        userService.deleteBatch(ids);
    }

    @GetMapping(value = "/currentUserInfo")
    @ApiOperation(value = "获取当前登录用户信息")
    public SysUserDetailVO getCurrentUserInfo() {
        return userService.getCurrentUserInfo();
    }

    @GetMapping(value = "/userMenu")
    @ApiOperation(value = "获取用户菜单")
    public List<SysMenu> getCurrentUserMenu() {
        return userService.getCurrentUserMenu();
    }

    @PutMapping(value = "/password")
    @ApiOperation(value = "修改密码")
    @OperationLog(value = "修改密码")
    public void updatePassword(@RequestBody PasswordDTO passwordDTO) {
        userService.updatePassword(passwordDTO);
    }

    @GetMapping(value = "/online")
    @ApiOperation(value = "查看在线用户")
    public PageResultVO<OnlineUser> listOnlineUsers(@Validated PageQuery pageQuery, String keywords) {
        return userService.listOnlineUsers(pageQuery, keywords);
    }

    @PostMapping(value = "/kick")
    @OperationLog(value = "踢人下线")
    @ApiOperation(value = "踢人下线")
    public void kick(String token) {
        userService.kick(token);
    }
}
